<template>
    <div>
      <div style="margin: 10px 0">
        <el-input style="width: 200px" placeholder="请输入讨论ID" suffix-icon="el-icon-search" v-model="post_id"></el-input>
        <el-input style="width: 200px" placeholder="请输入内容" suffix-icon="el-icon-message" class="ml-5" v-model="content"></el-input>
        <el-input style="width: 200px" placeholder="请输入回复ID" suffix-icon="el-icon-search" class="ml-5" v-model="user_id"></el-input>
        <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
        <el-button type="warning" @click="reset">重置</el-button>
      </div>
  
      <div style="margin: 10px 0" >
        <el-popconfirm
            class="ml-5"
            confirm-button-text='确定'
            cancel-button-text='我再想想'
            icon="el-icon-info"
            icon-color="red"
            comment_id="您确定批量删除这些数据吗？"
            @confirm="delBatch"
        >
          <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
        </el-popconfirm>
      </div>
  
      <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'"  @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="post_id" label="讨论ID" width="140"></el-table-column>
        <el-table-column prop="content" label="内容" width="600"></el-table-column>
        <el-table-column prop="user_id" label="回复ID" width="100"></el-table-column>
        <el-table-column prop="time" label="回复时间"></el-table-column>
        <el-table-column label="操作"  width="200" align="center">
          <template slot-scope="scope">
            <el-popconfirm
                class="ml-5"
                confirm-button-text='确定'
                cancel-button-text='我再想想'
                icon="el-icon-info"
                icon-color="red"
                comment_id="您确定删除吗？"
                @confirm="del(scope.row.id)"
            >
              <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div style="padding: 10px 0">
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[2, 5, 10, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
      </div>
  

    </div>
  </template>
  
  <script>
  export default {
    name: "comment",
    data() {
      return {
        tableData: [],
        total: 0,
        pageNum: 1,
        pageSize: 10,
        post_id: "",
        content: "",
        user_id: "",
        form: {},
        dialogFormVisible: false,
        multipleSelection: []
      }
    },
    created() {
      this.load()
    },
    methods: {
      load() {
        this.request.get("/comment/page", {
          params: {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            post_id: this.post_id,
            content: this.content,
            user_id: this.user_id,
          }
        }).then(res => {
          const data = res.data
          console.log("评论")
          console.log(data.code)
          this.tableData = data.data.list
          this.total = data.data.total
  
        })
      },
      del(id) {
        this.request.delete("/comment/" + id).then(res => {
          if (res.data.code === 200) {
            this.$message.success("删除成功")
            this.load()
          } else {
            this.$message.error("删除失败")
          }
        })
      },
      handleSelectionChange(val) {
        console.log(val)
        this.multipleSelection = val
      },
      delBatch() {
        let ids = this.multipleSelection.map(v => v.id)  // [{}, {}, {}] => [1,2,3]
        this.request.post("/comment/del/batch", ids).then(res => {
          if (res.data.code === 200) {
            this.$message.success("批量删除成功")
            this.load()
          } else {
            this.$message.error("批量删除失败")
          }
        })
      },
      reset() {
        this.post_id = ""
        this.content = ""
        this.user_id = ''
        this.load()
      },
      handleSizeChange(pageSize) {
        console.log(pageSize)
        this.pageSize = pageSize
        this.load()
      },
      handleCurrentChange(pageNum) {
        console.log(pageNum)
        this.pageNum = pageNum
        this.load()
      },
    }
  }
  </script>
  
  
  <style>
  .headerBg {
    background: #eee!important;
  }
  </style>